using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cam;
using net_work.IService.Cam;

namespace net_work.Controllers.Cam;

[Route("api/[controller]")]
[ApiController]
public class MessageController(ILogger<MessageController> logger, IMessageService messageService) : ControllerBase
{
    /// <summary>
    /// 获取消息列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListMessages([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取消息列表");
        var result = await messageService.GetMessagesPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取消息信息
    /// </summary>
    /// <param name="messageId">消息ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetMessageById([FromQuery] long messageId)
    {
        logger.LogInformation("获取消息信息");
        return await messageService.SelectByIdAsync(messageId) is { } message
            ? AjaxResult.Success(message)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增消息
    /// </summary>
    /// <param name="messageAddRequest">新增消息实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize]
    public async Task<IActionResult> AddMessage([FromBody] MessageAddRequest messageAddRequest)
    {
        logger.LogInformation("新增消息");
        var messageId = await messageService.InsertMessage(messageAddRequest);
        return messageId > 0
            ? AjaxResult.Success(new { messageId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 查看消息
    /// </summary>
    /// <param name="messageIds">消息ID列表</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize]
    public async Task<IActionResult> UpdateMessage([FromBody] List<long> messageIds)
    {
        logger.LogInformation("修改消息");
        return await messageService.UpdateMessage(messageIds) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除消息
    /// </summary>
    /// <param name="ids">消息ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize]
    public async Task<IActionResult> DeleteMessage([FromBody] List<long> ids)
    {
        logger.LogInformation("删除消息");
        return await messageService.DeleteMessageByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}